// 获取用户名getUsername
function getUsername() {
    // 发送请求
    $.ajax({
        url: "http://127.0.0.1:8023/user/username",
        type: "GET",
        dataType: "JSON",
        contentType: "application/x-www-form-urlencoded",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}
// 判断用户是否登录
function isLogin() {
    // 发送请求
    $.ajax({
        url: "http://127.0.0.1:8023/user/isLogin",
        type: "GET",
        dataType: "JSON",
        contentType: "application/x-www-form-urlencoded",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}
// 用户登出
function logout() {
    // 发送请求
    $.ajax({
        url: "http://127.0.0.1:8023/user/logout",
        type: "GET",
        dataType: "JSON",
        contentType: "application/x-www-form-urlencoded",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}

// 授权验证
function validate(provideOrg,rid) {
    // console.log(provideOrg,rid);
    // 发送请求
    $.ajax({
        url: "http://127.0.0.1:8023/abac/ask",
        type: "GET",
        dataType: "JSON",
        contentType: "application/x-www-form-urlencoded",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        data: {
            provideOrg: provideOrg,
            rid: rid
        },
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}

// 消息加密
function messageEncryption() {
    var reqParam = {
        age: 24,
        name: "zhangsan"
    }
    $.ajax({
        url: "http://127.0.0.1:8023/message/encryption/广州区管/104",
        type: "POST",
        dataType: "JSON",
        contentType: "application/json",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        data: JSON.stringify(reqParam),
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}
// 消息加密XML格式
function messageEncryptionForXML() {
    var xmlObject = "<user>" +
        "<name>zhangsan</name>" +
        "<age>24</age>" +
        "</user>"
    $.ajax({
        url: "http://127.0.0.1:8023/message/encryption_xml/广州区管/104",
        type: "POST",
        dataType: "JSON",
        contentType: "application/xml",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        data: xmlObject,
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}

// 消息解密
function messageDecryption(encryptedText) {
    var reqParam = {
        topicId: 104,
        provideOrg: "广州区管",
        encryptedText: encryptedText
    }
    $.ajax({
        url: "http://127.0.0.1:8023/message/decryption",
        type: "POST",
        dataType: "JSON",
        contentType: "application/x-www-form-urlencoded",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        data: reqParam,
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}

// 保存秘钥
function saveKey() {
    var reqParam = {
        "subscriptionId": 100,
        "provideOrg": "广州区管",
        "privateKey": "gqYzpA30BeqZt/Fp7iMstkHovBkm+A",
        "createBy": "test",
        "updateBy": "test"
    }
    $.ajax({
        url: "http://127.0.0.1:8023/message/save_key",
        type: "POST",
        dataType: "JSON",
        contentType: "application/json",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        data: JSON.stringify(reqParam),
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}

// 删除秘钥
function deleteKey() {
    var reqParam = {
        "subscriptionId": 100,
        "provideOrg": "广州区管"
    }
    $.ajax({
        url: "http://127.0.0.1:8023/message/delete_key",
        type: "POST",
        dataType: "JSON",
        contentType: "application/json",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("satoken",localStorage.getItem("satoken"));
        },
        data: JSON.stringify(reqParam),
        success: function(res) {
            console.log(res);
            // 展示
            var jdata = JSON.stringify(res, null, 4);
            $(".showMessage").html("<pre>"+jdata+"</pre>");
        }
    });
}